package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.common.R;
import com.example.pojo.entity.ApUser;
import com.example.service.ApUserService;
import com.example.service.impl.ApUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.example.utils.JsonChangeObject.JsonObject;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String jsonStr = JsonObject(req);
        ApUser apUser = JSON.parseObject(jsonStr, ApUser.class);
        ApUserService apUserService = new ApUserServiceImpl();
        R<ApUser> user = null;
        try {
            user = apUserService.login(apUser.getPhone(), apUser.getPassword());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //返回结果
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(JSON.toJSONString(user));
    }
}
